import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { UIManager } from '../common/UIManager';
import HeroApi from '../api/HeroApi';
import { Hero, LocationStrList } from '../entity/Hero';
import { SelectHeroItem } from './SelectHeroItem';
import { EventManager } from '../event/EventManager';
import { UIHeader } from '../common/UIHeader';
import { PrefabEnum } from '../common/PrefabEnum';
const { ccclass, property } = _decorator;

@ccclass('SelectHero')
export class SelectHero extends Component {
    @property(UIHeader)
    uiHeader:UIHeader
    @property(Label)
    title:Label
    @property(Node)
    content:Node
    @property(Prefab)
    selectHeroItem:Prefab

    private loc:number

    start() {
        this.uiHeader.setData('选择英雄')
        EventManager.instance.on('选择英雄close', this.onClose, this)
    }

    async setData(loc:number){
        this.loc = loc
        this.title.string = `请选择【${LocationStrList[loc]}】位置的英雄`
    
        await UIManager.instance.showLoading()
        let res = await HeroApi.Execute({op:1})
        UIManager.instance.closeLoading()

        let items = res.data.items
        let heroList = Hero.getListInHeroYard(items)

        this.content.destroyAllChildren()
        for(let hero of heroList){
            if(hero.ad == 0){
                continue
            }
            let node = instantiate(this.selectHeroItem)
            this.content.addChild(node)
            node.getComponent(SelectHeroItem).setData(hero, loc)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.SelectHero)
    }
}


